//
//  HourseQueryVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/11.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HourseQueryVc.h"
#import "BdTV.h"
#import "BrokerDataMange.h"
#import "UserModal.h"
#import "MJRefresh.h"
#import "StoreSelecteCell.h"
#import "PremisesModel.h"







@interface HourseQueryVc ()
@property (strong, nonatomic) BrokerDataMange *dm;
@property (strong, nonatomic) NSArray *dataArray;
@end



@implementation HourseQueryVc
@synthesize dm,dataArray,selectedFlag,delegate;



- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self initView];
    
    [self refreshData];
    
}



-(void)initView{
    [super initView];

    BdTV *titleView = [BdTV creatInstanceWithTitle:@"楼盘查询"];
    [titleView.backBtn setTag:11];
    [titleView.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleView.doneBtn removeFromSuperview];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:titleView];
    
    dm = [BrokerDataMange new];
}


-(void)refreshData{
    [super refreshData];
    
    
    [self.nodataCell setState:nodataCellStateLoading];
    
     __weak typeof(self) baseSelf = self;
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
       ResulteMod *resultMod = [dm premiseslistWithUserCode:[UserModal shareUserModel].userCode];
       
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            [baseSelf.tableView.header endRefreshing];
            
            if (resultMod.resltState==ResultNetError) {
                
                [baseSelf.nodataCell setState:nodataCellStateNetError];
            }else if (resultMod.resltState==ResultFail){
                [baseSelf.nodataCell setCodeErrorTip:resultMod.resultCode];
            }else{
                
                baseSelf.dataArray = resultMod.dataMod;
                
                if(baseSelf.dataArray.count>0){
                }else{
                    [baseSelf.nodataCell setState:nodataCellStateNodata];
                }
            }
            
            [baseSelf.tableView reloadData];
            
        });
    });
}






-(void)btnOnClick:(UIButton *)sender{
    
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }



}



#pragma mark tableView delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return tableView.frame.size.height;
    }
    
    return [StoreSelecteCell getHight:nil withTableView:nil];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return self.nodataCell;
    }
    
    StoreSelecteCell *cell = [StoreSelecteCell cellWithTableView:tableView];
    PremisesModel *cellData = [dataArray objectAtIndex:indexPath.row];
    
    [cell setIsChecked:[cellData.premisesTitle isEqual:selectedFlag]];
    
    [cell setTitle:cellData.premisesTitle];
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return;
    }

     PremisesModel *cellData = [dataArray objectAtIndex:indexPath.row];
    
    if ([delegate respondsToSelector:@selector(hourseBeSelected:withHourseName:)]) {
        [delegate hourseBeSelected:[cellData.premisesId intValue] withHourseName:cellData.premisesTitle];
    }
}




@end
